package com.java3_17;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Ticket {
    private int ticket;

    public Ticket() {

    }

    public Ticket(int ticket) {
        this.ticket = ticket;
    }

    public int getTicket() {
        return ticket;
    }

    public void setTicket(int ticket) {
        this.ticket = ticket;
    }

    //默认为非公平锁
//    Lock lock = new ReentrantLock(true);

//    public void delTicket() {
//        lock.lock();
//        try {
//            if (ticket > 0) {
//                System.out.println(Thread.currentThread().getName() + "卖出一张票" + " " + ticket);
//                ticket--;
//            }
//        } finally {
//            lock.unlock();
//        }
//    }

//    public void saleTicket() {
//       synchronized (this){
//           System.out.println("外层");
//           synchronized (this){
//               System.out.println("中层");
//               synchronized (this){
//                   System.out.println("内层");
//               }
//           }
//       }
//    }

    Lock lock = new ReentrantLock(true);
    public void saleTicket(){
        lock.lock();
        try{
            System.out.println("外层");
            lock.lock();
            try{
                System.out.println("中层");
                lock.lock();
                try{
                    System.out.println("内层");
                }finally {
                    lock.unlock();
                }
            }finally {
                lock.unlock();
            }
        }finally {
            lock.unlock();
        }
    }
}
